#pragma once

#include "../comm/log.hpp"
#include "../comm/util.hpp"

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <assert.h>
#include <fstream>
#include<mysql/mysql.h>

// 根据题目列表将题目信息加载进内存,以及进行数据交互

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    // 题目结构体
    struct Question
    {
        string number; // 题目编号
        string title;  // 题目标题
        string star;   // 难度：简单 中等 困难
        string desc;   // 题目表述
        string header; // 代码格式
        string tail;   // 题目测试用例（必须与header拼接形成完整代码，给后端编译运行判题）
        int cpu_limit; // 时间要求
        int mem_limit; // 空间要求
    };


    const string oj_questions = "oj_questions";//表名

    const string host = "127.0.0.1";
    const string user = "oj_client";
    const string password = "1061040041Xlj";
    const string db = "oj";
    const int port = 3306;


    class Model
    {
    public:
        Model()
        {}

        ~Model() {}

        //从数据库提取数据
        bool QueryMySql(const string& sql, vector<Question>* out)
        {
            MYSQL* my = mysql_init(nullptr);

            //连接数据库
            if(mysql_real_connect(my,host.c_str(),user.c_str(),password.c_str(),db.c_str(),port,nullptr,0)==nullptr)
            {
                LOG(FATAL)<<"连接数据库失败..."<<"\n";
                return false;
            }
            LOG(INFO)<<"连接数据库成功！"<<"\n";

            mysql_set_character_set(my,"utf8");//设定字符集

            //执行sql语句
            if(0 != mysql_query(my,sql.c_str()))
            {
                LOG(WARNING)<<sql<<" execute error!"<<"\n";
                return false;
            }

            //指令操作成功，读取数据
            MYSQL_RES* res = mysql_store_result(my);//提取结果
            int rows = mysql_num_rows(res);//获取行数
            int cols = mysql_num_fields(res);//获取列数

            struct Question q;
            for(int i = 0; i<rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);//获取行数据，自动更新
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit =  atoi(row[7]);

                out->push_back(q);
            }

            //free(res);
            mysql_close(my);//关闭连接
            return true;
        }

        // 获取全部题目
        bool GetAllQuestions(vector<Question> *out)
        {
            string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql,out);
        }

        // 获取一个题目信息
        bool GetOneQuestion(const string &number, Question *q)
        {
            bool res = false;
            string sql = "select * from ";
            sql += oj_questions;
            sql += " where id=";
            sql += number;
            vector<Question> result;
            if(QueryMySql(sql,&result))
            {
                if(result.size()==1)
                {
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }
        
    };
}
